<?php

/**

 * 考勤管理
 * 
 * @access  public 
 * @author  zhangyingheng<zhang_yh@jsumt.com.cn> 
 */

namespace Index\Controller;

use Think\Controller;

class AttendanceController extends BaseController {

    public $webid;
    public $pin;

    public function _initialize() {
        parent::_initialize();
        $this->title = '考勤管理';
        $webid = M('web')->where('webname="考勤管理"')->getField('id');
        $this->webid = $webid;
    }

    private function getAttLists($month = '', $day = '', $year = '', $limit = '') {
        $month = $month ? $month : date('m');
        $day = $day ? date('d') : '31';
        $year = $year ? $year : date('Y');
        $pin = M('userinfo')->where('user_id="' . session('user_id') . '"')->getField('pin');
        $this->pin = $pin;
        $map['pin'] = $pin;
        //本月非工作日天数
        $restDays = getWeekendDays($year . '-' . $month . '-01', $year . '-' . $month . $day);
        //本月应出勤天数
        $mustWorkDays = $day - $restDays;
        $start = $year . '-' . $month . '-01 00:00:00';
        $end = $year . '-' . $month . '-' . $day . ' 23:59:59';
        $map['stime'] = array('between', [$start, $end]);
        if ($limit) {
            $p = I('get.p', 1, 'intval');
            $start = $limit * ($p - 1);
            $currAttLists = M('inoutdata')->where($map)->limit($start, $limit)->select();
        } else {
            $currAttLists = M('inoutdata')->where($map)->select();
        }
        return $currAttLists;
    }

    public function index() {
        //同步user_id
//        $users = M('userinfo')->select();
//        foreach($users as $key=>$row){
//            $map['USER_NAME'] = $row['name'];
//            $data['user_id'] = M('user')->where($map)->getField('user_id');
//            M('userinfo')->where('name="'.$row['name'].'"')->save($data);
//        }
        $lists = $this->getAttLists(date('m'), date('d'));
        $attDaysCount = count(getMinOrMax($lists, 1));  //实院出勤天数
        //迟到天数
        $lateDaysCount = count(getMinOrMax($lists, 2));

        //缺打卡次数
        $loseDaysCount = count(getMinOrMax($lists, 3));

        //当日打卡情况
        $map['pin'] = $this->pin;
        $map['stime'] = array('between', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')]);
        $row = M('inoutdata')->where($map)->select();
        $today = getMinOrMax($row, 4);

        //昨日打卡情况
        $yday = date('d') - 1;
        $map['stime'] = array('between', [date('Y-m-' . $yday . ' 00:00:00'), date('Y-m-' . $yday . ' 23:59:59')]);
        $row = M('inoutdata')->where($map)->select();
        $yestoday = getMinOrMax($row, 4);

        //早退次数
        $earlyCount = count(getMinOrMax($lists, 5));

        //剩余年假
        $this->yearLeave = M('userinfo')->where('user_id="' . session('user_id') . '"')->getField('able_year');

        //请假次数
        $this->leaveCount = $this->getAttendData(1, 1);

        //出差次数
        $this->evectionCount = $this->getAttendData(2, 1);

        //外出次数
        $this->outCount = $this->getAttendData(3, 1);


        //显示当前年、月
        $this->currentShow = date('m') . '月，' . date('Y') . '年';
        $this->month = date('m');
        $this->year = date('Y');
        $this->day = date('d');
        $this->attDaysCount = $attDaysCount;
        $this->lateDaysCount = $lateDaysCount;
        $this->loseDaysCount = $loseDaysCount;
        $this->today = $today;
        $this->yestoday = $yestoday;
        $this->earlyCount = $earlyCount;
        $this->display();
    }

    /*
     * 获取考勤次数或明细
     * param $table string  数据表名  1：attend_leave  2:attend_evection 3:attend_out 4:attendance_overtime
     * param $returnType  int     返回类型  1：count  0:select
     * param $year  int     年      空：当年
     * param $month int     月份    空：当月
     */

    private function getAttendData($table, $returnType, $year = '', $month = '') {
        $year = $year ? $year : date('Y');
        $month = $month ? $month : date('m');

        $map['USER_ID'] = session('user_id');
        $start = $year . '-' . $month . '-01 00:00:00';
        $end = $year . '-' . $month . '-31 23:59:59';

        switch ($table) {
            case 1:     //请假
                $map['LEAVE_DATE1'] = array('between', [$start, $end]);
                $map['LEAVE_DATE2'] = array('between', [$start, $end]);
                $count = M('attend_leave')->where($map)->getField('leave_duration', true);
                $count = !is_array($count) ? $count[$count] : $count;
                if ($count) {
                    $count = array_sum($count);
                } else {
                    $count = 0;
                }
                $lists = M('attend_leave')->where($map)->select();
                break;
            case 2:     //出差
                $map['EVECTION_DATE1'] = array('between', [$start, $end]);
                $map['EVECTION_DATE2'] = array('between', [$start, $end]);
                $startDay = M('attend_evection')->where($map)->getField('evection_date1', true);
                $endDay = M('attend_evection')->where($map)->getField('evection_date2', true);
                $startDay = !is_array($startDay) ? $startDay[$startDay] : $startDay;
                $endDay = !is_array($endDay) ? $endDay[$endDay] : $endDay;
                $count = 0;
                for ($i = 0; $i < count($startDay); $i++) {
                    $count += (strtotime(substr($endDay[$i], 0, 10)) - strtotime(substr($startDay[$i], 0, 10))) / 86400;
                }
                $lists = M('attend_evection')->where($map)->select();
                break;
            case 3:  //外出
                $map['SUBMIT_TIME'] = array('between', [$start, $end]);
                $startTime = M('attend_out')->where($map)->getField('out_time1', true);
                $endTime = M('attend_out')->where($map)->getField('out_time2', true);
                $startTime = !is_array($startTime) ? $startTime[$startTime] : $startTime;
                $endTime = !is_array($endTime) ? $endTime[$endTime] : $endTime;
                $outDay = substr(M('attend_out')->where($map)->getField('submit_time'), 0, 10);
                $count = 0;
                for ($i = 0; $i < count($startTime); $i++) {
                    $sec = strtotime($outDay . ' ' . $endTime[$i]) - strtotime($outDay . ' ' . $startTime[$i]);
                    $sec = secsToStr($sec, 2);
                    $count += $sec;
                }
                $lists = M('attend_out')->where($map)->select();
                break;
        }
        if ($returnType) {
            return $count;
        } else {
            return $lists;
        }
    }

    public function lists() {
        $type = I('type');
        $month = I('month');
        $year = I('year');
        $lists = $this->getAttLists($month, '', $year);
        switch ($type) {
            case 1: //出勤
                $data = getMinOrMax($lists, 1);
                $name = '出勤';
                break;
            case 2:
                $name = '迟到';
                $data = getMinOrMax($lists, 2);
                break;
            case 3:
                $name = '早退';
                $data = getMinOrMax($lists, 5);
                break;
            case 4:
                $name = '缺卡';
                $data = getMinOrMax($lists, 3);
                break;
            case 5:
                $name = '请假';
                $data = $this->getAttendData(1, 0, $year, $month);
                break;
            case 6:
                $name = '出差';
                $data = $this->getAttendData(2, 0, $year, $month);
                break;
            case 7:
                $name = '外出';
                $data = $this->getAttendData(3, 0, $year, $month);
                break;
        }

        $this->type = $type;
        $this->name = $name;
        $this->data = $data;
        $this->display();
    }

    public function attLists() {
        $type = I('type');
        $month = I('month');
        $year = I('year');
        $lists = $this->getAttLists($month, '', $year);
        switch ($type) {
            case 5:
                $name = '请假';
                $data = $this->getAttendData(1, 0, $year, $month);
                foreach ($data as $key => $row) {
                    $data[$key]['day'] = substr($row['leave_date1'], 0, 10);
                    $data[$key]['name'] = $row['leave_type2'];
                    $data[$key]['count'] = $row['leave_duration'];
                }
                break;
            case 6:
                $name = '出差';
                $data = $this->getAttendData(2, 0, $year, $month);
                foreach($data as $key=>$row){
                    $data[$key]['day'] = substr($row['evection_date1'], 0, 10) . '~' . substr($row['evection_date2'], 5, 6);
                    $data[$key]['name'] = $row['evection_dest'];
                    $data[$key]['count'] = (strtotime(substr($row['evection_date2'], 0, 10)) - strtotime(substr($row['evection_date1'], 0, 10))) / 86400;
                }
                break;
            case 7:
                $name = '外出';
                $data = $this->getAttendData(3, 0, $year, $month);
                foreach ($data as $key => $row) {
                    $data[$key]['day'] = substr($row['submit_time'], 0, 10);
                    $data[$key]['name'] = $row['out_type'];
                    $sec = strtotime($data[$key]['day'] . ' ' . $row['out_time2']) - strtotime($data[$key]['day'] . ' ' . $row['out_time1']);
                    $data[$key]['count'] = secsToStr($sec, 2);
                }
                break;
        }

        $this->type = $type;
        $this->name = $name;
        $this->data = $data;
        $this->display();
    }

    //获取其他月份考勤信息
    public function getOtherMounthData() {
        $month = I('month');
        $year = I('year');
        if (strlen($month) == 1) {
            $month = '0' . $month;
        }
        $return['show'] = $month . '月，' . $year . '年';
        $return['year'] = $year;
        $return['month'] = $month;

        $pin = M('userinfo')->where('user_id="' . session('user_id') . '"')->getField('pin');
        $map['pin'] = $pin;
        //指定月非工作日天数
        $startDay = $year . '-' . $month . '-01 00:00:00';
        $endDay = $year . '-' . $month . '-31 23:59:59';
        $restDays = getWeekendDays($startDay, $endDay);
        //指定月应出勤天数
        $mustWorkDays = date('d') - $restDays;
        $map['stime'] = array('between', [$startDay, $endDay]);
        $lists = M('inoutdata')->where($map)->select();

        $return['attDaysCount'] = count(getMinOrMax($lists, 1, $month, $year));  //实院出勤天数
        //迟到天数
        $return['lateDaysCount'] = count(getMinOrMax($lists, 2, $month, $year));

        //缺打卡次数
        $return['loseDaysCount'] = count(getMinOrMax($lists, 3, $month, $year, '31'));

        //早退次数
        $return['earlyCount'] = count(getMinOrMax($lists, 5, $month, $year));

        //剩余年假
        $return['yearLeave'] = M('userinfo')->where('user_id="' . session('user_id') . '"')->getField('able_year');
        
        //请假天数
        $return['leaveCount'] = $this->getAttendData(1, 1,$year,$month);
        
        //出差次数
        $return['evectionCount'] = $this->getAttendData(2, 1,$year,$month);

        //外出次数
        $return['outCount'] = $this->getAttendData(3, 1,$year,$month);

        $this->ajaxReturn($return);
    }

    //获取日期
    public function getDate() {
        $date = strtotime(I('date'));
        $newData = I('date') . ' ' . getChineseWeek(date('w', $date));
        $this->ajaxReturn($newData);
    }

}
